//
//  UserLoginViewController.m
//  MacWeb
//
//  Created by 小贝 on 16/3/16.
//  Copyright © 2016年 Boye. All rights reserved.
//

#import "UserLoginViewController.h"
@interface UserLoginViewController ()<NSComboBoxDataSource,NSComboBoxDelegate,NSTableViewDataSource,NSTableViewDelegate>
@property (nonatomic,strong)NSMutableArray *userArray;//用户名数组
@property (nonatomic,strong)NSMutableArray *userIDArr;//用户id数组
//@property (nonatomic,copy)NSString *Id;
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSScrollView *myScrollView;
@end
@implementation UserLoginViewController
-(NSMutableArray *)userArray{
    if (_userArray ==nil) {
        _userArray = [NSMutableArray array];
    }
    return _userArray;
}
-(NSMutableArray *)userIDArr{
    if (_userIDArr == nil) {
        _userIDArr = [NSMutableArray array];
    }
    return _userIDArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    for(NSDictionary *dic in self.userArr){
        [self.userArray addObject:[dic objectForKey:@"name"]];
        [self.userIDArr addObject:[dic objectForKey:@"id"]];
    }
    [self.tableView setBackgroundColor:[NSColor blackColor]];
    [self.myScrollView setAlphaValue:0.5];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
    
    //根据用户的多少重置myScrollView的大小
    CGFloat myScrollView_x = self.myScrollView.frame.origin.x;
    CGFloat myScrollView_y = self.myScrollView.frame.origin.y;
    CGFloat myScrollView_W = self.myScrollView.frame.size.width;
    self.myScrollView.frame = CGRectMake(myScrollView_x, myScrollView_y, myScrollView_W, 63+self.userArray.count*40);
}
#pragma mark---NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return self.userArray.count + 2;
}
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    return NO;
}
#pragma mark---NSTableViewDelegate
- ( NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row{
    
    if(row == 0){
        NSButton *btn = [[NSButton alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 214, 63))];
        [btn setFont:[NSFont systemFontOfSize:20]];
        btn.focusRingType = NSFocusRingTypeDefault;
        [btn setTitle:@"请选择帐号"];
        [btn setImage:[NSImage imageNamed:@"bg"]];
        [btn setBordered:NO];
        [btn setTransparent:NO];
        [NSButton setTextColor:btn andColor:[NSColor whiteColor]];
        return btn;
    }else{
        NSButton *btn = [[NSButton alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 214, 30))];
        [btn setFont:[NSFont systemFontOfSize:18]];
        [NSButton setTextColor:btn andColor:[NSColor whiteColor]];
        [btn setTarget:self];
        [btn setTag:row];
        [btn setAction:@selector(clickAction:)];
        [btn setBordered:NO];
        [btn setTitle:self.userArray[row-2]];
        return btn;
    }
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    if(row == 0){
        return 30;
    }else if(row == 1){
        return 9;
    }else{
        return 30;
    }
}
-(CGFloat)tableView:(NSTableView *)tableView sizeToFitWidthOfColumn:(NSInteger)column{
    
    return 0;
}
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row{
    
    if (row == 0) {
        return NO;
    }else
        return YES;
}

#pragma mark-- 选择用户跳转页面
- (void)clickAction:(NSButton *)btn{
    NSString *url = [NSString stringWithFormat:@"%@?number=%@&userId=%@&date=20160101&bookId=0",CheckUserPath,self.userName,self.userIDArr[btn.tag -1]];
    NSString *urlStr = [NSString URLEncodedString:url];
    
    [HHNetRequest requestWithGet:urlStr parmeter:nil successBlock:^(id resultValue) {
        if ([[resultValue objectForKey:@"bookCount"] isEqualToString:@"0"]) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.yOffset = 100.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:2];
            NSString *message = [resultValue objectForKey:@"errMsg"];
            hud.labelText = message;
        }else{
            ViewController *mainVC = [self.storyboard instantiateControllerWithIdentifier:@"MainVC"];
            mainVC.type = @"多用户";
            mainVC.userID = self.userIDArr[btn.tag - 1];
            mainVC.userName = self.userName;
            [[NSApp mainWindow] setContentViewController:mainVC];
            float width = [NSScreen mainScreen].frame.size.width;
            float height =[NSScreen mainScreen].frame.size.height;
            [[NSApp mainWindow] setContentSize:NSMakeSize(width, height)];
        }
    } failBlock:^(id failedBlock) {
        NSLog(@"%@",failedBlock);
    }];
    
}


@end
